package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderList")
public class OrderListController {


    @Reference
    private OrderService orderService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return orderService.findPage(queryPageBean);

    }

    //添加电话预约
    @RequestMapping("/addOrderByTelephone")
    public Result addOrderByTelephone(@RequestBody Map<String, Object> map, Integer[] setmealIds) {

        if (setmealIds == null || setmealIds.length == 0) {
            return new Result(false, "没有选择套餐");
        }
        //明确预约方式
        map.put("orderType", Order.ORDERTYPE_TELEPHONE);

        Result result = null;
        try {
            result = orderService.addOrderByTelephone(map, setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            if (null == result) {

                return new Result(false, MessageConstant.ORDER_FAIL);
            }
        }
        if (result.isFlag()) {//预约成功,给用户发送短信

            String phoneNumber = (String) map.get("phoneNumber");
            try {
//                SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,phoneNumber, (String) map.get("orderDate"));

                System.out.println("[已预约成功] + 手机号是:" + phoneNumber + "预约的时间是" + (String) map.get("orderDate"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;

    }

    //确定预约
    @RequestMapping("/confirmOrder")
    public Result confirmOrder(@RequestBody Map map) {

        String telephone = (String) map.get("phoneNumber");

        try {
            orderService.confirmOrder(map);

            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, "success");
            return new Result(true, MessageConstant.CONFIRM_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CONFIRM_ORDER_FAIL);
        }
    }


    //取消预约
    @RequestMapping("/cancelOrder")
    public Result cancelOrder(@RequestBody Map map) {

        String telephone = (String) map.get("phoneNumber");

        try {
            orderService.cancelOrder(map);

            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, "fail");
            return new Result(true, MessageConstant.CANCEL_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CANCEL_ORDER_FAIL);
        }
    }

    //批量确定预约
    @RequestMapping("/multiConfirmOrder")
    public Result multiConfirmOrder(@RequestBody List<Map> list) {
        //判断参数是否为空
        if (list == null) {
            return new Result(false, MessageConstant.MULTIPARTCONFIRM_ORDER_FAIL);
        }

        try {

            //批量确定预约
            orderService.multiConfirmOrder(list);
            //批量发短信通知
            for (Map map : list) {
                //获取手机号
                String telephone = (String) map.get("phoneNumber");

                //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,"success");
                System.out.println("批量确定预约==================");
            }
            return new Result(true, MessageConstant.MULTIPARTCONFIRM_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.MULTIPARTCONFIRM_ORDER_FAIL);
        }
    }

    //批量取消预约
    @RequestMapping("/multiCancelOrder")
    public Result multiCancelOrder(@RequestBody List<Map> list){
        //判断参数是否为空
        if (list == null) {
            return new Result(false, MessageConstant.MULTIPARTCONFIRM_ORDER_FAIL);
        }

        try {

            //批量取消预约
            orderService.multiCancelOrder(list);
            //批量发短信通知
            for (Map map : list) {
                //获取手机号
                String telephone = (String) map.get("phoneNumber");

                //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,"success");
                System.out.println("批量确定预约==================");
            }
            return new Result(true, MessageConstant.MULTIPARTCONCEL_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.MULTIPARTCONCEL_ORDER_FAIL);
        }
    }
}
